<template>
  <div class="user-bind-device">
    <div class="user-bind-device__title">用户绑定设备</div>
    <div class="user-bind-device__form">
      <el-form :model="form" :rules="rules" ref="form">
        <el-form-item label="产品二维码qcode:" prop="qrcode">
          <el-input type="text" v-model="form.qrcode"  placeholder="请输入二维码"></el-input>
        </el-form-item>
        <el-form-item class="sumbit">
          <el-button type="danger" @click="submitForm" class="login-submit" >{{submiting ? '提交中...': '提交'}}</el-button>
        </el-form-item>
      </el-form>
      <el-button type="danger" @click="go" class="login-submit" >进入控制页面</el-button>
    </div>
  </div>
</template>
<script>
import api from '@/api'

export default {
  name: 'UserBindDevice',
  data () {
    return {
      submiting: false,
      form: {
        qrcode: ''
      },
      rules: {
        qrcode: [
          { required: true, message: '产品二维码qrcode', trigger: 'blur' }
        ]
      }
    }
  },

  computed: {
    userId () {
      return window.localStorage.userId
    }
  },
  methods: {
    submitForm () {
      if (this.submiting) return
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          this.submiting = true
          try {
            let condition = {
              qrcode: this.form.qrcode
            }
            await api.qrcode_subscribe(this.userId, condition)
            this.$message({
              message: '绑定成功',
              type: 'success'
            })
            this.$router.push('/xlink')
          } catch (err) {
            this.$message.error('绑定失败' + err.response.data.error.msg)
          }
          this.submiting = false
        }
      })
    },

    go () {
      this.$router.push('/xlink')
    }
  }
}
</script>
<style lang="stylus" scoped>
.user-bind-device 
  width 500px
  border 1px solid #eee
  border-radius 12px
  margin 100px auto 1000px
  padding-bottom 20px
  box-shadow: 0 3px 6px rgba(0,0,0,.15)
  &__title
    font-size: 20px;
    margin 30px 0 25px
  &__form
    box-sizing border-box
    padding 0 80px
    .account-tip
      text-align left
    .message-tip
      text-align left
    .login-submit
      width 260px
      margin-top 5px
      border-radius 12px
    .user-bind-device-wrap
      a
        color #a5a4a4
        text-decoration none
</style>
